package com.xintoucloud.pbs.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xintoucloud.pbs.model.DmDto;
import com.xintoucloud.pbs.model.JmiDto;
import com.xintoucloud.pbs.service.DevelopMemberService;
import com.xintoucloud.pbs.service.JoinMeetingInformationService;
@Controller@RequestMapping("/dmsdug")
public class DevelopMemberController {
	@Autowired
	private DevelopMemberService developMemberService;
	
	@Autowired
	private JoinMeetingInformationService joinMeetingInformationService;
	
	@RequestMapping("/dmdto")
	public String partyMember() {
		return "dmlist3";
		
	}
	
	@RequestMapping("dmlist")
	public String List(Integer oid,Model m) {
		m.addAttribute("alldm", developMemberService.list(oid));
		return "dmlist";
	}
	
	@RequestMapping("/dmdlist")
	@ResponseBody
	public Map<String,Object> List3() {
		int oid = 1;
		List<DmDto> dmdlist = developMemberService.list2(oid);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 0);
		map.put("data", dmdlist);
		return map;
	}
    @PostMapping("/adddm")
    @ResponseBody
    public String add(HttpServletRequest request) {
    	DmDto newdmd = new DmDto();
    	newdmd.setDmnum(request.getParameter("dmnum"));
    	newdmd.setName(request.getParameter("name"));
    	newdmd.setSex(request.getParameter("sex"));
    	newdmd.setNation(request.getParameter("nation"));
    	newdmd.setIdnum(request.getParameter("idnum"));
    	newdmd.setEducation(request.getParameter("education"));
    	newdmd.setWorkUnit(request.getParameter("workUnit"));
    	newdmd.setWorkJob(request.getParameter("workJob"));
    	newdmd.setWorkJobType(request.getParameter("workJobType"));
    	newdmd.setFamilyAddress(request.getParameter("familyAddress"));
    	newdmd.setPic(request.getParameter("pic"));
    	newdmd.setOid(1);
    	newdmd.setDmsid(1);
    	
    	boolean result = developMemberService.save(newdmd);
    	return "dmdto";
    }
    
    @PostMapping("/dmdelete")
    @ResponseBody
    public void delete(HttpServletRequest request) {
    	developMemberService.delete(Integer.parseInt(request.getParameter("bid")));
    }
    
    @PostMapping("/dmupdate")
    @ResponseBody
    public String update(HttpServletRequest request) {
    	DmDto updatedmd = new DmDto();
    	updatedmd.setBid(Integer.parseInt(request.getParameter("bid")));
    	updatedmd.setDmnum(request.getParameter("dmnum"));
    	updatedmd.setName(request.getParameter("name"));
    	updatedmd.setSex(request.getParameter("sex"));
    	updatedmd.setNation(request.getParameter("nation"));
    	updatedmd.setIdnum(request.getParameter("idnum"));
    	updatedmd.setEducation(request.getParameter("education"));
    	updatedmd.setWorkUnit(request.getParameter("workUnit"));
    	updatedmd.setWorkJob(request.getParameter("workJob"));
    	updatedmd.setWorkJobType(request.getParameter("workJobType"));
    	updatedmd.setFamilyAddress(request.getParameter("familyAddress"));
    	updatedmd.setPic(request.getParameter("pic"));
    	updatedmd.setOid(1);
    	updatedmd.setDmsid(1);
    	
    	developMemberService.update(updatedmd);
    	return "dmdto";
    }
    
    @PostMapping("/addinfo")
    @ResponseBody
    public String addinfo(HttpServletRequest request) throws ParseException {
    	JmiDto jmidto = new JmiDto();
    	
    	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	Date createTime = format.parse(request.getParameter("createTime"));
    	Date meetingTime = format.parse(request.getParameter("meetingTime"));
    	
    	jmidto.setBid(Integer.parseInt(request.getParameter("bid")));
    	jmidto.setCreateTime(createTime);
    	jmidto.setMeetingTime(meetingTime);
    	jmidto.setShouldNum(Integer.parseInt(request.getParameter("shouldNum")));
    	jmidto.setRealNum(Integer.parseInt(request.getParameter("realNum")));
    	jmidto.setMeetingResult(request.getParameter("meetingResult"));
    	jmidto.setFirstRecommended(request.getParameter("firstRec"));
    	jmidto.setFirstRecPosition(request.getParameter("firstRecPosition"));
    	jmidto.setSecondRecommended(request.getParameter("secondRec"));
    	jmidto.setSecondRecPosition(request.getParameter("secondRecPosition"));
    	jmidto.setMeetingNum(request.getParameter("meetingNum"));
    	jmidto.setMtid(Integer.parseInt(request.getParameter("mtid")));
    	
    	joinMeetingInformationService.save(jmidto);
    	
    	return "dmdto";
    }
    /*@PostMapping("/upload")
    @ResponseBody
    public String addmpic(HttpServletRequest request) throws IOException {
    	// 创建一个基于磁盘的文件项
    	DiskFileItemFactory factory = new DiskFileItemFactory();

    	// Configure a repository (to ensure a secure temp location is used)配置存储库（以确保使用安全的临时位置
    	ServletContext servletContext = (ServletContext) factory.getRepository().getCanonicalFile();
    			//getServletConfig().getServletContext();
    	File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
    	factory.setRepository(repository);

    	// Create a new file upload handler 创建一个新的文件上传处理程序
    	ServletFileUpload upload = new ServletFileUpload(factory);

    	// Parse the request 解析请求
    	try {
			List<FileItem> items = upload.parseRequest(request);
			for(FileItem item : items) {
				if(item.isFormField()) {
					String fieldName = item.getFieldName();
					String value = item.getString("utf-8");
				}else {
					String fileName = item.getName();//获取文件名 不使用原文件名 目的是为了得到文件后缀
					String surfix = fileName.substring(fileName.lastIndexOf("."));//  后缀 从“.”开始截取
					UUID uuid = UUID.randomUUID();// 用UUID生成前缀  目前UUID中有“_”
					String prefix = uuid.toString().replaceAll("_", "");
					String saveFileName = prefix+surfix;
					String path = servletContext.getRealPath("/image/meeting_pic");//获取图片文件夹的真实路径
					System.out.println(path);
					InputStream is = item.getInputStream();//用IO获取输入流
					OutputStream os = new FileOutputStream(new File(path+File.separator+saveFileName));
					byte[] b = new byte[1024];
					int len;
					while((len = is.read(b))!=-1) {
						os.write(b, 0, len);
					}
					is.close();
					os.close();
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
    	return null;
    }*/

    
}
